var index = 0;
var point = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageindex: 0,
    pagepoint:0,
    remainQ:100,
    questionLib: "",
    questioncontent: "",
    questionImageUrl: "",
    item1: '',
    item2: '',
    item3: '',
    item4: '',
    explains: '',
    answer: '',
    answer_status: 'none',
    timer: '',//定时器名字
    countDownNum: '600'//倒计时初始值
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    wx.request({
      url: 'http://v.juhe.cn/jztk/query?subject=' + options.subject + '&model=' + options.model + '&testType=rand&=&key=53af3ee326412e2a96d1dadace4efdfc',

      success: (res) => {
        console.log(res)
        this.setData({
          pageindex: index,
          questionLib: res.data.result,
          questioncontent: res.data.result[index].question,
          questionImageUrl: res.data.result[index].url,
          item1: res.data.result[index].item1,
          item2: res.data.result[index].item2,
          item3: res.data.result[index].item3,
          item4: res.data.result[index].item4,
          explains: res.data.result[index].explains,
          answer: res.data.result[index].answer,
        })
        console.log(this.data.questionLib[index].question)
      }, fail: (res) => {
        wx.showToast({
          title: '获取题库失败',
          icon: 'none'
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.countDown();
  },
  countDown: function () {
    let that = this;
    let countDownNum = that.data.countDownNum;//获取倒计时初始值
    //如果将定时器设置在外面，那么用户就看不到countDownNum的数值动态变化，所以要把定时器存进data里面
    that.data.timer = setInterval(function () {//这里把setInterval赋值给变量名为timer的变量
      //在倒计时还未到0时，这中间可以做其他的事情，按项目需求来
      if (countDownNum == 0) {
        wx.showModal({
          title: '时间到',
          content: '总成绩:' + point + ' 是否退出本堂考试？',
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.switchTab({
                url: '/pages/knowknow/knowknow?point=' + point
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        clearInterval(that.data.timer);
        //这里特别要注意，计时器是始终一直在走的，如果你的时间为0，那么就要关掉定时器！不然相当耗性能
        //因为timer是存在data里面的，所以在关掉时，也要在data里取出后再关闭
        // clearInterval(that.data.timer);
        //关闭定时器之后，可作其他处理codes go here
      } else {
        //每隔一秒countDownNum就减一，实现同步
        countDownNum--;
        //然后把countDownNum存进data，好让用户知道时间在倒计着
        that.setData({
          countDownNum: countDownNum
        })
      }
    }, 1000)
  },
  onNext: function () {

    index++;
    this.setData({
      pageindex: index,
      remainQ : 100 - index,
      questioncontent: this.data.questionLib[index].question,
      questionImageUrl: this.data.questionLib[index].url,
      item1: this.data.questionLib[index].item1,
      item2: this.data.questionLib[index].item2,
      item3: this.data.questionLib[index].item3,
      item4: this.data.questionLib[index].item4,
      explains: this.data.questionLib[index].explains,
      answer: this.data.questionLib[index].answer,
      answer_status: 'none'
    })
    if(index == 99)
    {
      wx.showModal({
        title: '你已经完成考试',
        content: '总成绩:' + point +' 是否退出本堂考试？',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
            wx.switchTab({
              url: '/pages/knowknow/knowknow?point=' + point
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  },
  onLast: function () {
    index--;
    this.setData({
      pageindex: index,
      questioncontent: this.data.questionLib[index].question,
      questionImageUrl: this.data.questionLib[index].url,
      item1: this.data.questionLib[index].item1,
      item2: this.data.questionLib[index].item2,
      item3: this.data.questionLib[index].item3,
      item4: this.data.questionLib[index].item4,
      explains: this.data.questionLib[index].explains,
      answer: this.data.questionLib[index].answer,
      answer_status: 'none'
    })
  },
  examChangeBox: function () {
    this.setData({
      answer_status: 'none'
    })
  
  },
  onCommit:function()
  {
    wx.showModal({
      content: '是否结束本堂考试?',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.showModal({
            title: '用户交卷成功',
            content: '总成绩:' + point + ' 是否退出本堂考试？',
            success(res){
              if(res.confirm)
              {
                wx.switchTab({
                  url: '/pages/knowknow/knowknow?point=' + point
                })
              } else if (res.cancel) {
                console.log('用户点击取消')}
            }
          })
         
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  onA: function () {
    this.examChangeBox()
    if (this.data.answer == 1) {
      point++;
      this.setData({
        pagepoint: point
      })     
    } 
    this.onNext()
  },
  onB: function () {
    this.examChangeBox()
    if (this.data.answer == 2) {
      point++;
      this.setData({
        pagepoint: point
      })
    }
    this.onNext()
  },
  onC: function () {
    this.examChangeBox()
    if (this.data.answer == 3) {
      point++;
      this.setData({
        pagepoint: point
      })
    }
    this.onNext()
  },
  onD: function () {
    this.examChangeBox()
    if (this.data.answer == 4) {
      point++;
      this.setData({
        pagepoint: point
      })
    }
    this.onNext()
  },
})